from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os
import jieba
from wordcloud import WordCloud, STOPWORDS

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# 1.读入txt文本数据
txt = open('./1.txt',encoding="utf-8").read()
# print(text)
# 2.结巴中文分词，生成字符串，默认精确模式，如果不通过分词，无法直接生成正确的中文词云
cut_text = jieba.cut_for_search(txt)

# 必须给个符号分隔开分词结果来形成字符串,否则不能绘制词云
result = " ".join(cut_text)
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
alice_mask = np.array(Image.open(path.join(d, "flower.jpg")))
stopwords = set(STOPWORDS)
wc = WordCloud(font_path=r'.\simhei.ttf', background_color="pink", max_words=2000, mask=alice_mask,
               stopwords=stopwords)
# generate word cloud
wc.generate(result)


# store to file
# wc.to_file(r"wordcloud.png")
# show
plt.imshow(wc)
# 关闭图像坐标系
plt.axis("off")
plt.show()
